package com.qf.java2007.web.controller;

import com.qf.java2007.pojo.BookInfo;
import com.qf.java2007.pojo.ReaderInfo;
import com.qf.java2007.service.BookInfoService;
import com.qf.java2007.service.ReaderCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Author AzureSky_X
 * @Date 2021/3/9 16:48
 * @Version 1.0
 */
@Controller
public class ReaderCardController {
    @Autowired
    private BookInfoService bookInfoService;

    @Autowired
    private ReaderCardService readerCardService;
    /**
     * 显示头部导航栏
     * @return
     */
    @GetMapping("/reader_header.html")
    public String readerHeader(){
        return "reader_header";
    }

    /**
     * 到借阅人主界面
     * @return
     */
    @GetMapping("/reader_main.html")
    public String readerMain(){
        return "reader_main";
    }

    /**
     * 查询借阅人个人信息
     * @return
     */
    @GetMapping("/reader_info.html")
    public ModelAndView readerInfo(Long readerId){
        ReaderInfo readerInfo= readerCardService.findReaderCardById(readerId);
        ModelAndView mav=new ModelAndView("reader_info");
        mav.addObject("readerInfo",readerInfo);

        return mav;
    }

    /**
     * 到达修改界面，并显示信息
     * @param readerId
     * @return
     */
    @GetMapping("/reader_info_edit.html")
    public ModelAndView readerInfoEdit(Long readerId){
        ReaderInfo readerInfo= readerCardService.findReaderCardById(readerId);
        ModelAndView mav=new ModelAndView("reader_info_edit");
        mav.addObject("readerInfo",readerInfo);
        return mav;
    }

    /**
     * 修改借阅人信息并跳转到信息展示页面
     * @param readerInfo
     * @return
     */
    @PostMapping("/update")
    public ModelAndView update(ReaderInfo readerInfo){
        readerCardService.update(readerInfo);

        ModelAndView mav = new ModelAndView("reader_info");
        mav.addObject("succ", "修改成功！！！");
        return mav;
    }

    /**
     * 到更新密码界面
     * @param readerId
     * @return
     */
    @GetMapping("/reader_repasswd.html")
    public ModelAndView readerRepasswd(Long readerId){
        //ReaderInfo readerInfo= readerCardService.findReaderCardById(readerId);
        ModelAndView mav=new ModelAndView("reader_repasswd");
        //mav.addObject("readerInfo",readerInfo);
        return mav;
    }

    /**
     * 更新密码
     * @param
     * @return
     */
    @PostMapping("/updatePwd")
    public ModelAndView updatePwd(String reNewPasswd, String readerId){
        readerCardService.updatePwd(reNewPasswd,readerId);

        ModelAndView mav=new ModelAndView("reader_repasswd");


        return mav;
    }
    /**
     * 列出所有书籍信息
     * @return
     */
    @GetMapping("/reader_books.html")
    public ModelAndView list(){

        List<BookInfo> list = bookInfoService.findAll();
        ModelAndView mav=new ModelAndView("reader_books");
        mav.addObject("bookInfos",list);
        return mav;
    }
    /**
     * 查看书籍详情
     * @param bookId
     * @return
     */
    @GetMapping("/detail.html")
    public ModelAndView detail(Long bookId){

        BookInfo bookInfo=bookInfoService.findByIdWithClass(bookId);
        ModelAndView mav=new ModelAndView("reader_book_detail");
        mav.addObject("bookInfo",bookInfo);
        return mav;
    }
    /**
     * 根据用户输入的关键字模糊查询出书籍信息
     * @param searchWord
     * @return
     */
    @PostMapping("/search")
    public ModelAndView search(String searchWord){
        List<BookInfo> list = bookInfoService.findByNameOrAuthor(searchWord);
        ModelAndView mav = new ModelAndView("reader_books");
        mav.addObject("bookInfos", list);
        mav.addObject("searchWord", searchWord);
        return mav;
    }
    /**
     * 跳转到添加读者界面
     * @param
     * @return
     */
    @GetMapping("/reader_add.html")
    public ModelAndView readerAdd(){

        ModelAndView mav = new ModelAndView("admin_reader_add");

        return mav;
    }



}
